<?php
require_once(DIR_FACEBOOK_SDK.'/src/facebook.php');
require_once(DIR_MODEL.'/Comments.php');
require_once(DIR_MODEL.'/Posts.php');
date_default_timezone_set("Asia/Ho_Chi_Minh");
class facebookApi{

    private $facebook;
    private $posts;
    private $comments;
    private $debug;
    private $db;

    public function __construct($is_new=false){
        $config = array();
        $config['appId'] = '241377276037921';
        $config['secret'] = '5b82b012fa67b582e6538da6c72bf3ce';
        $this->posts = new \Post_facebook\Posts();
        $this->comments = new \Post_facebook\Comments();
        $this->debug = $_SESSION['debug'];
        $this->db = $_SESSION['db'];
        if($is_new){
            $this->facebook = new Facebook($config);
            $this->facebook->setExtendedAccessToken();
            $this->facebook->getAccessToken();
        }
    }

    public function getInformationGroup($until=''){
        $group_infos = $this->facebook->api('241619362662034/feed?limit='.DEFAULT_FACEBOOK_LIMIT.$until);
        return !empty($group_infos) ? $group_infos : array();
    }

    public function updatePaging($until,$since){
        $this->db->query("UPDATE fb_paging SET paging_next = '".$until."',paging_previous='".$since."' WHERE paging_id = 1");
    }

    public function pushDataIntoDB($data_get){
        foreach($data_get['data'] as $item){
            $this->posts->insert($item);
            if(isset($item['comments']['data']) && !empty($item['comments']['data'])){
                foreach($item['comments']['data'] as $item_comments){
//                    $this->debug->w($item_comments,0);
                    $this->comments->insert($item_comments,$item['id']);
                }
            }
        }
    }

    public function getInformationGroupByDatabase($data = array()){
        $posts = $this->posts->getPostList($data);
        //$this->debug->w($posts,0);
        return $posts;
    }
}
?>